package com.example.enterprisemanager.mvvm.fragment.account.register;

import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.ben.User;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RegisterModel extends BaseModel {

    private Disposable mRegisterUserDisposable;

    /**
     * 账号密码注册
     */
    public void signUp(User user, Consumer<User> success, Consumer<Exception> fail) {

        mRegisterUserDisposable = user.signUp(new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(user);

                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mRegisterUserDisposable != null) {
            mRegisterUserDisposable.dispose();
        }
    }
}
